package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IShoppingGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cskaoyan.mall.constant.ShoppingRetCode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@Service
public class IShoppingGoodsServiceImpl implements IShoppingGoodsService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public AllProductResponse getShoppingGoods(AllProductRequest request) {

        AllProductResponse itemResponse = new AllProductResponse();

        //参数校验
        request.requestCheck();

        // 分页插件
        PageHelper.startPage(request.getPage(), request.getSize());

        if ("1".equals(request.getSort())) {
            if (request.getPriceGt() == null) {
                request.setPriceGt(0);
            }
            if (request.getPriceLte() == null) {
                request.setPriceLte(Integer.MAX_VALUE);
            }
            if (request.getPriceLte() == Integer.MAX_VALUE && request.getPriceGt() == 0) {
                request.setPriceLte(null);
                request.setPriceGt(null);
            }
            List<Item> items = itemMapper.selectItemFront(null, "price", "asc", request.getPriceGt(), request.getPriceLte());
            PageInfo<Item> pageInfo = new PageInfo<>(items);
            long total = pageInfo.getTotal();
            List<ProductDto> itemDtos = productConverter.items2Dto(items);

            //成功执行
            itemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            itemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            itemResponse.setProductDtoList(itemDtos);
            itemResponse.setTotal(total);
            return itemResponse;
        } else if ("-1".equals(request.getSort())) {
            if (request.getPriceGt() == null) {
                request.setPriceGt(0);
            }
            if (request.getPriceLte() == null) {
                request.setPriceLte(Integer.MAX_VALUE);
            }
            if (request.getPriceLte() == Integer.MAX_VALUE && request.getPriceGt() == 0) {
                request.setPriceLte(null);
                request.setPriceGt(null);
            }
            List<Item> items = itemMapper.selectItemFront(null, "price", "desc", request.getPriceGt(), request.getPriceLte());
            PageInfo<Item> pageInfo = new PageInfo<>(items);
            long total = pageInfo.getTotal();
            List<ProductDto> shoppingGoodsDtos = productConverter.items2Dto(items);

            //成功执行
            itemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            itemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            itemResponse.setProductDtoList(shoppingGoodsDtos);
            itemResponse.setTotal(total);
            return itemResponse;
        }

        if (request.getPriceGt() == null) {
            request.setPriceGt(0);
        }
        if (request.getPriceLte() == null) {
            request.setPriceLte(Integer.MAX_VALUE);
        }
        if (request.getPriceLte() == Integer.MAX_VALUE && request.getPriceGt() == 0) {
            request.setPriceLte(null);
            request.setPriceGt(null);
        }
        List<Item> items = itemMapper.selectItemFront(null, "created", "asc", request.getPriceGt(), request.getPriceLte());
        PageInfo<Item> pageInfo = new PageInfo<>(items);
        long total = pageInfo.getTotal();
        List<ProductDto> shoppingGoodsDtos = productConverter.items2Dto(items);

        //成功执行
        itemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        itemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        itemResponse.setProductDtoList(shoppingGoodsDtos);
        itemResponse.setTotal(total);
        return itemResponse;
    }

    @Override
    public RecommendResponse getShoppingRecommend() {

        RecommendResponse recommendResponse = new RecommendResponse();
        HashSet<PanelDto> panelDtoHashSet = new HashSet<>();

        List<Panel> panels = panelMapper.selectPanelContentById(6);

        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(6);
        List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);

        for (Panel panel : panels) {

            PanelDto panelDto = contentConverter.panel2Dto(panel);

            panelDto.setPanelContentItems(panelContentItemDtos);

            panelDtoHashSet.add(panelDto);
        }

        recommendResponse.setPanelContentItemDtos(panelDtoHashSet);
        recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        return recommendResponse;
    }

    @Override
    public ProductDetailResponse getShoppingProduct(ProductDetailRequest request) {

        ProductDetailResponse response = new ProductDetailResponse();

        ProductDetailDto productDetailDto = new ProductDetailDto();

        Item item = itemMapper.selectByPrimaryKey(request.getId());

        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());

        productDetailDto.setProductId(item.getId());
        productDetailDto.setSalePrice(item.getPrice());
        productDetailDto.setProductName(item.getTitle());
        productDetailDto.setSubTitle(item.getSellPoint());
        productDetailDto.setLimitNum(Long.decode(item.getLimitNum().toString()));
        productDetailDto.setProductImageBig(item.getImageBig());
        productDetailDto.setDetail(itemDesc.getItemDesc());
        String[] images = item.getImages();
        List<String> list = Arrays.asList(images);
        productDetailDto.setProductImageSmall(list);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        response.setProductDetailDto(productDetailDto);

        return response;
    }
}
